package com.proyecto.track.bo;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

@Entity
public class Task {
	private Long id;
	private String title;
	private String description;
	private Double hours;
	private TaskStatus taskStatus;
	private TaskType taskType;
	private Period period;
	private List<Coment> comments = new ArrayList<Coment>();
	private List<UserTask> userTasks = new ArrayList<UserTask>();
	
	public Task(String title, String description, Double hours){
		this.title = title;
		this.description = description;
		this.hours = hours;
	}
	
	public Task(){
		
	}
	
	@Id
	@GeneratedValue
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Double getHours() {
		return hours;
	}

	public void setHours(Double hours) {
		this.hours = hours;
	}

	@ManyToOne(fetch=FetchType.LAZY)
	public TaskStatus getTaskStatus() {
		return taskStatus;
	}

	public void setTaskStatus(TaskStatus taskStatus) {
		this.taskStatus = taskStatus;
	}

	@ManyToOne(fetch=FetchType.LAZY)
	public TaskType getTaskType() {
		return taskType;
	}

	public void setTaskType(TaskType taskType) {
		this.taskType = taskType;
	}

	@OneToOne(cascade={CascadeType.ALL})
	public Period getPeriod() {
		return period;
	}

	public void setPeriod(Period period) {
		this.period = period;
	}

	@OneToMany(mappedBy="task", cascade={CascadeType.ALL})
	public List<Coment> getComments() {
		return comments;
	}

	public void setComments(List<Coment> comments) {
		this.comments = comments;
	}

	@OneToMany(mappedBy="task", cascade={CascadeType.ALL})
	public List<UserTask> getUserTasks() {
		return userTasks;
	}

	public void setUserTasks(List<UserTask> userTasks) {
		this.userTasks = userTasks;
	}
	
	public void removeUser(UserTask userTask) {
		userTask.setActive(false);
	}

	public void addUsers(UserPerson userPerson) {
		if (userPerson.userPersonIsInTask(this.id)) {
			userPerson.getUserTaskById(this.id).setActive(true);
		} else {
			UserTask userTask = new UserTask(userPerson, this, true);
			this.userTasks.add(userTask);
			userPerson.getUserTasks().add(userTask);
		}

	}

}
